/**
 * 
 */
package dms.service.imp;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dms.dto.DormitoryDTO;
import dms.model.Building;
import dms.model.Campus;
import dms.model.Dormitory;
import dms.model.util.PageIterator;
import dms.repository.BuildingRepository;
import dms.repository.DormitoryRepository;
import dms.service.DormitoryService;

/**
 * @author 李健
 *
 */
public class DormitoryServiceImpl implements DormitoryService {

	private static final Log log = LogFactory.getLog(DormitoryServiceImpl.class);
	
	private BuildingRepository buildingRepository = null;
	
	private DormitoryRepository dormitoryRepository = null;
	
	/**
	 * 
	 */
	public DormitoryServiceImpl() {
		super();
	}

	/**
	 * @param buildingRepository
	 * @param dormitoryRepository
	 */
	public DormitoryServiceImpl(BuildingRepository buildingRepository,
			DormitoryRepository dormitoryRepository) {
		super();
		this.buildingRepository = buildingRepository;
		this.dormitoryRepository = dormitoryRepository;
	}

	/**
	 * @return the buildingRepository
	 */
	public BuildingRepository getBuildingRepository() {
		return buildingRepository;
	}

	/**
	 * @param buildingRepository the buildingRepository to set
	 */
	public void setBuildingRepository(BuildingRepository buildingRepository) {
		this.buildingRepository = buildingRepository;
	}

	/**
	 * @return the dormitoryRepository
	 */
	public DormitoryRepository getDormitoryRepository() {
		return dormitoryRepository;
	}

	/**
	 * @param dormitoryRepository the dormitoryRepository to set
	 */
	public void setDormitoryRepository(DormitoryRepository dormitoryRepository) {
		this.dormitoryRepository = dormitoryRepository;
	}

	@Override
	public Dormitory createDormitory(DormitoryDTO dormitoryInfo) {
		Dormitory dormitory = new Dormitory();
		Building building = this.buildingRepository.findById(dormitoryInfo.getBuilding());
		
		dormitory.setCapacity(dormitoryInfo.getCapacity());
		dormitory.setDescription(dormitoryInfo.getDescription());
		dormitory.setName(dormitoryInfo.getName());
		dormitory.setTelephone(dormitoryInfo.getTelephone());
		
		dormitory.setBuilding(building);
		
		return this.dormitoryRepository.createDormitory(dormitory);
	}

	@Override
	public void deleteById(Long id) {
		this.dormitoryRepository.deleteById(id);
		
	}

	@Override
	public void deleteDormitories(List<Long> ids) {
		this.dormitoryRepository.deleteDormitories(ids);
		
	}

	@Override
	public PageIterator<Dormitory> findByBuilding(int start, int count, Building building) {
		return this.dormitoryRepository.findByBuilding(start, count, building);
	}

	@Override
	public Dormitory findByBuildingAndName(Building building, String name) {
		PageIterator<Dormitory> iter = this.dormitoryRepository.findByBuilding(0, 1, building);
		Dormitory dormitory = null;
		while (iter.hasNext()) {
			dormitory = iter.next();
		}
		return dormitory;
	}

	@Override
	public Dormitory findById(Long id) {
		return this.dormitoryRepository.findById(id);
	}

	@Override
	public Dormitory updateDormitory(DormitoryDTO dormitoryInfo) {
		Dormitory dormitory = this.findById(dormitoryInfo.getId());
		Building building = this.buildingRepository.findById(dormitoryInfo.getBuilding());
		
		dormitory.setCapacity(dormitoryInfo.getCapacity());
		dormitory.setDescription(dormitoryInfo.getDescription());
		dormitory.setName(dormitoryInfo.getName());
		dormitory.setTelephone(dormitoryInfo.getTelephone());
		
		dormitory.setBuilding(building);
		
		return this.dormitoryRepository.updateDormitory(dormitory);
	}

	@Override
	public PageIterator<Dormitory> findByCampus(int start, int count,
			Campus campus) {
		return this.dormitoryRepository.findByCampus(start, count, campus);
	}

	@Override
	public PageIterator<Dormitory> findAll(int start, int count) {
		return this.dormitoryRepository.findAll(start, count);
	}

}
